<?php
namespace app\admin\model;
use app\common\model\CommonModel;
use app\common\model\Category;
use app\common\model\Attribute;

class CategoryModel extends CommonModel
{
	/**
	* 添加分类信息
	*/
	public static function setCategory($info)
	{
		$data['category_name'] = $info['category_name'];
		$data['fid'] = $info['fid'];
		if(!(new Category())->serCategory($data))
		{
			return back(config('state.STATUS_CODE_TWO'),config('state.STATUS_MSG_TWO'));
		}
		return back(config('state.STATUS_CODE_ONE'),config('state.STATUS_MSG_ONE'));
	}
	/**
	* 修改分类信息
	*/
	public static function editCategory($info)
	{
		$data['category_name'] = $info['category_name'];
		$data['fid'] = $info['fid'];
		if((new Category())->updateCategory(['category_id'=>$info['id']],$data) === false)
		{
			return back(config('state.STATUS_CODE_TWO'),config('state.STATUS_MSG_TWO'));
		}
		return back(config('state.STATUS_CODE_ONE'),config('state.STATUS_MSG_ONE'));
	}
	/**
	* 获取分类信息
	*/
	public static function getCategory()
	{
		return self::getTree((new Category())->categoryList([],'category_id,category_name,fid,is_show'), CommonModel::NUMBER_ZERO);
	}
	private static function getTree($data, $pId)
	{
		$tree = [];
		foreach($data as $k => $v)
		{
			if($v['fid'] == $pId)
			{
				if($piddata = self::getTree($data, $v['category_id']))
				{
					$v['fid'] = $piddata;
				}
				$tree[] = $v;
			}
		}
		return $tree;
	}
	/**
	* 获取当前分类信息
	*/
	public static function getCategoryInfo($id)
	{
		return (new Category())->CategoryInfo(['category_id'=>$id]);
	}
	/**
	* 获取父级分类
	*/
	public static function FCate()
	{
		return (new Category())->categoryList(['fid'=>CommonModel::NUMBER_ZERO],'category_id,category_name');
	}
    public static function categoFcate($info)
    {
        $cat = explode(',',$info['id']);
        $data['data'] = (new Category())->categoryList(['fid'=>$cat[0]],'category_id,category_name');
        $data['fname'] = $cat[1];
        return $data;
    }
	/**
	* 删除
	*/
	public static function delCategory($id)
	{
		if((new Category())->delCategory(['category_id'=>$id]) === false)
		{
			return back(config('state.STATUS_CODE_TWO'),config('state.STATUS_MSG_TWO'));
		}
		return back(config('state.STATUS_CODE_ONE'),config('state.STATUS_MSG_ONE'));
	}
	/**
	* 二级分类
	*/
	public static function towCategory()
	{
		$Category = new Category();
		$categoryid = $Category->getCategoryArr(['fid'=>CommonModel::NUMBER_ZERO],'category_id');
		return $Category->categoryList(['fid'=>['in',$categoryid]],'category_id,category_name');
	}
	/**
	* 获取下级分类
	*/
	public static function lowerCate($id)
	{
		if($id == CommonModel::NUMBER_ZERO)
		{
			return back(config('state.STATUS_CODE_THREE'),config('state.STATUS_MSG_THREE'));
		}
		if(!$catedata = (new Category())->categoryList(['fid'=>$id],'category_id,category_name'))
		{
			return back(config('state.STATUS_CODE_THREE'),config('state.STATUS_MSG_THREE'));
		}
		return back(config('state.STATUS_CODE_ONE'),config('state.STATUS_MSG_ONE'),$catedata);
	}
	/**
	* 添加属性
	*/
	public static function setAttribute($info)
	{
		$data['attribute_name'] = $info['attribute_name'];
		$data['category_id'] = $info['category_id'];
		$data['type'] = $info['type'];
		if(!(new Attribute())->serAttribute($data))
		{
			return back(config('state.STATUS_CODE_TWO'),config('state.STATUS_MSG_TWO'));
		}
		return back(config('state.STATUS_CODE_ONE'),config('state.STATUS_MSG_ONE'));
	}
	/**
	* 属性列表
	*/
	public static function getAttributeList($limit,$page)
	{
		$attributeList = (new Attribute())->getAttributeList($limit,$page);
		$Category = new Category();
		foreach($attributeList['data'] as $i=>$attribute)
		{
			$attributeList['data'][$i]['category_id'] = $Category->getCategoryField(['category_id'=>$attribute['category_id']],'category_name');
			$attributeList['data'][$i]['type'] = self::AttriType($attribute['type']);
			$attributeList['data'][$i]['is_show'] = $attribute['is_show']==CommonModel::NUMBER_ZERO?'正常':'关闭';
		}
		return $attributeList;
	}
	/**
	* 属性
	*/
	private static function AttriType($type)
	{
		switch($type)
		{
			case CommonModel::NUMBER_ZERO:return '品牌';break;
			case CommonModel::NUMBER_ONE:return '内存';break;
			case CommonModel::NUMBER_TWO:return '属性';break;
			case CommonModel::NUMBER_THREE:return '颜色';break;
		}
	}
	/**
	* 获取当前属性信息
	*/
	public static function getAttributeInfo($id)
	{
		return (new Attribute())->AttributeInfo(['attribute_id'=>$id]);
	}
	/**
	* 修改属性信息
	*/
	public static function editAttribute($info)
	{
		$data['attribute_name'] = $info['attribute_name'];
		$data['category_id'] = $info['category_id'];
		$data['type'] = $info['type'];
		if((new Attribute())->updateAttribute(['attribute_id'=>$info['id']],$data) === false)
		{
			return back(config('state.STATUS_CODE_TWO'),config('state.STATUS_MSG_TWO'));
		}
		return back(config('state.STATUS_CODE_ONE'),config('state.STATUS_MSG_ONE'));
	}
	/**
	* 删除
	*/
	public static function delAttribute($id)
	{
		if((new Attribute())->delAttribute(['attribute_id'=>$id]) === false)
		{
			return back(config('state.STATUS_CODE_TWO'),config('state.STATUS_MSG_TWO'));
		}
		return back(config('state.STATUS_CODE_ONE'),config('state.STATUS_MSG_ONE'));
	}
	/**
	* 获取下级属性
	*/
	public static function lowerIbute($id)
	{
		$Attribute = new Attribute();
		$where['category_id'] = $id;
		$where['is_show'] = CommonModel::NUMBER_ZERO;
		$data = [];
		/*品牌*/
		$where['type'] = CommonModel::NUMBER_ZERO;
		$data['pingpai'] = $Attribute->attributeList($where,'attribute_name');
		/*内存*/
		$where['type'] = CommonModel::NUMBER_ONE;
		$data['neicun'] = $Attribute->attributeList($where,'attribute_name');
		/*属性*/
		$where['type'] = CommonModel::NUMBER_TWO;
		$data['shuxing'] = $Attribute->attributeList($where,'attribute_name');
		/*颜色*/
		$where['type'] = CommonModel::NUMBER_THREE;
		$data['yanse'] = $Attribute->attributeList($where,'attribute_name');
		return $data;
	}
}
